﻿using System.IO;

using Aliyun.OSS;

using Fortunate.API.BASE;
using Fortunate.API.OSS;
using Fortunate.Application.Entities;
using Fortunate.Common.Enum;
using Fortunate.Common.Helper;
using Fortunate.Common.Utility;
using Fortunate.ViewModel;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

using ZXing;

namespace Fortunate.API.Controllers
{
    /// <summary>
    /// 测试
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : BaseApiController
    {
        /// <summary>
        /// 阿里云oss上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostOssUpload(IFormFile file)
        {
            //MemoryStream stream = new MemoryStream();
            //stream.SetLength(file.Length);
            //file.CopyTo(stream);
            //stream.Seek(0, SeekOrigin.Begin);
            // 上传文件。
            PutObjectResult result = MyAliyunOssClient.OssUpload(file.FileName, file.OpenReadStream());
            //stream.Close();
            return Success("上传成功");
        }

        /// <summary>
        /// 阿里云oss文件下载
        /// </summary>
        /// <param name="objectName">oss根据文件名称来下载</param>
        /// <returns></returns>
        [HttpGet("/DowOssFile")]
        public IActionResult PostDowOssFile(string objectName)
        {
            // 下载文件到流。OssObject包含了文件的各种信息，如文件所在的存储空间、文件名、元信息以及一个输入流。
            OssObject? obj = MyAliyunOssClient.DownloadOssFile(objectName);
            return File(obj.Content, "application/octet-stream", obj.Key);
        }
    }
}
